package com.itcast.bulls.stock.user.controller;

import com.itcast.bulls.stock.common.exception.BusinessException;
import com.itcast.bulls.stock.common.exception.ComponentException;
import com.itcast.bulls.stock.entity.user.TradeUser;
import com.itcast.bulls.stock.user.service.IStockUserService;
import com.itcast.stock.common.web.vo.ApiRespResult;
import com.itcast.stock.common.web.vo.user.TradeUserVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/open")
@Log4j2
public class StockUserOpenController {

    @Autowired
    private IStockUserService stockUserService;
    /**
     * 用户注册接口
     * @param tradeUser
     * @return
     */
    @RequestMapping( value = "/register",method = RequestMethod.POST)
    public ApiRespResult register(@Valid TradeUserVo tradeUser){
        ApiRespResult result = null;
        try {
            //调用用户的注册接口方法
            TradeUser newTradeUser = stockUserService.userRegister(tradeUser);
            System.out.println(newTradeUser.getName());
            result = ApiRespResult.success(newTradeUser);
            System.out.println(result);
        } catch (BusinessException e) {
            log.error(e.getMessage(),e);
            result = ApiRespResult.error(e.getErrorCodeEnum());
        } catch (ComponentException e) {
            log.error(e.getMessage(),e);
            result = ApiRespResult.error(e.getErrorCodeEnum());
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result = ApiRespResult.sysError(e.getMessage());
        }
        System.out.println(result);
        return result;
    }
}
